const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    navList: [],
    isloadmore: true, // 加载更多
    curId: '',
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    _this.setData(options, () => {
      let { id } = _this.data;
      _this.setData({
        curId: id || '',
      }, () => {
        _this.getNav();
      });
    });
    wx.setNavigationBarTitle({
      title: options.barTitle && decodeURIComponent(options.barTitle) || '主题活动',
    });
  },
  getNav() {
    let { curId } = this.data;
    $util.request({
      url: 'entry/wxapp/edu.activity.main',
      data: {
        page: this.data.page,
        psize: 50,
      },
      success: (res) => {
        let navList = res.list;
        navList = [...[{
          id: '',
          classifyname: '全部', 
          picture: '',
        }], ...navList];
        this.setData({
          navList,
          isloadmore: res.total > navList.length,
          page: res.total > navList.length ? ++this.data.page : this.data.page,
        }, () => {
          for (let i = 0, lg = navList.length; i < lg; i++) {
            if (String(curId) === String(navList[i].id)) {
              this.setData({
                index: i,
        });
              break;
            }
          }
        this.loadList();
        });
      },
    });
  },
  loadList() {
    $util.request({
      url: 'entry/wxapp/edu.activity.specialmain',
      data: {
        page: this.data.page,
        id: this.data.curId,
      },
      success: (res) => {
        let list = res.list;
        if (this.data.page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list,
          isloadmore: res.total > list.length,
          page: res.total > list.length ? ++this.data.page : this.data.page,
          isLoad: true,
        });
      },
    });
  },
  changeTab(e) {
    this.setData({
      curId: e.detail.obj.id,
      page: 1,
    });
    this.loadList();
  },
  navigateTo(e) {
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url,
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isloadmore) {
      this.loadList();
    }
  },
  onShareAppMessage() {
    let _this = this;
    let path = '/ws_party_const/pages/education/activity/index';
    let res = {
      path,
    };
    return res;
  },
});
